#include <bits/stdc++.h>

using namespace std;

int n, N, q;

int main()
{
    cin >> n >> N >> q;
    typedef pair<int, int> pii;
    unordered_map<int, pii> nodes; // num: time, iswrite
    vector<set<pii>> pqs(N + 1);   // time, num
    long long a;
    int o, t;

    while (q--)
    {
        cin >> o >> a;
        t = (a / n) % N;
        auto &pq = pqs[t];
        if (nodes.find(a) != nodes.end())
        {
            pq.insert({q, a});
            if (pq.find({nodes[a].first, a}) != pq.end())
                pq.erase({nodes[a].first, a});
            nodes[a] = {q, nodes[a].second || o};
            continue;
        }
        int diff = pq.size();
        if (diff >= n)
        {
            auto pqtt = pq.rbegin();
            auto pqt = *pqtt;
            pq.erase(pqt);
            if (nodes[pqt.second].second)
            {
                cout << 1 << " " << pqt.second << endl;
            }
            nodes.erase(pqt.second);
        }
        // 直接加入
        pq.insert({q, a});
        if (pq.find({nodes[a].first, a}) != pq.end())
            pq.erase({nodes[a].first, a});
        nodes[a] = {q, nodes[a].second || o};
        cout << 0 << " " << a << endl;
    }
}